package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbCunJiandingMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.PrisonerXjxxMapper;
import com.sinog.biz.service.business.BjgzJiandingInfoMainService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbCunJiandingResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @desc 年度鉴定实现类
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzJiandingInfoMainServiceImpl extends ServiceImpl<AbCunJiandingMapper,AbCunJiandingResultVo> implements BjgzJiandingInfoMainService {

    @Resource
    private AbCunJiandingMapper abCunJiandingMapper;
    @Resource
    private PrisonerXjxxMapper prisonerXjxxMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;

    /**
     * 保存鉴定信息
     * @param dataStr dataStr
     * @return Result
     */
    @Override
    public Result saveJdInfo(String dataStr,UserDTO user) {
        Date date = new Date();
        AbCunJiandingResultVo arb = JsonUtil.toBean(dataStr,AbCunJiandingResultVo.class);
        //新增
        if(StringUtils.isBlank(arb.getId())) {
            arb.setId(IdGen.strid());
            arb.setAdduserid(user.getDepartid());
            arb.setCreateUser(user.getUsername());
            arb.setCreateTime(date);
            Result flag = findjdnf(arb.getNiandu(),arb.getPriNum());
            if(Constants.Number.NUM_S_Z_Z_T.equals(flag.getCode())) {
                return Result.failure(ReturnCode.YEAR_NOT_ALLOW);
            }
        }
        arb.setUpdateUser(user.getUsername());
        arb.setUpdateTime(date);
        boolean tag = arb.insertOrUpdate();
        if(tag) {
            //年度鉴定情况应该在这判断
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取查看鉴定信息
     * @param priNum priNum
     * @param id id
     * @return Result
     */
    @Override
    public Result getJdViewBaseInfo(String priNum,String id) {
        Map<String,Object> map = abCunJiandingMapper.getJdViewBaseInfo(id);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 获取编辑鉴定信息
     * @param priNum priNum
     * @param id id
     * @return Result
     */
    @Override
    public Result getJdEditBaseInfo(String priNum,String id) {
        Map<String,Object> map = abCunJiandingMapper.getJdBaseInfo(id);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 年度鉴定 - 鉴定 - 删除
     * @param id id
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result toDelAppraise(String id,String priNum) {
        int num = abCunJiandingMapper.toDelAppraise(id);
        if(0 < num) {
            return Result.success("删除成功");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 年度鉴定 - 鉴定 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param map map
     * @return String
     */
    @Override
    public String getJdBaseInfoList(Integer pageIndex,Integer pageSize,Map<String,Object> map) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        //本系统的本地code前两位
        String flag = CodeUrlUtils.getLocalCodeStartTwo();
        map.put("flag",flag);
        page = page.setRecords(abCunJiandingMapper.getJdBaseInfoList(page,map));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 鉴定人员详细列表
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getPingguInfo(String priNum) {
        Map<String,Object> map = prisonerXjxxMapper.getPingguInfo(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 查找服刑人员的鉴定年份
     * @param priNum priNum
     * @param niandu niandu
     * @return Result
     */
    @Override
    public Result findjdnf(String niandu,String priNum) {
        AbCunJiandingResultVo arv = abCunJiandingMapper.selectOne(new QueryWrapper<AbCunJiandingResultVo>().lambda()
                                                                                                           .eq(AbCunJiandingResultVo::getPriNum,priNum)
                                                                                                           .eq(AbCunJiandingResultVo::getNiandu,niandu)
                                                                                                           .last("limit 1"));
        if(ObjectUtils.allNotNull(arv)) {
            return Result.failure(ReturnCode.YEAR_NOT_ALLOW);
        }
        return Result.success();
    }

    /**
     * 安置帮教管理 - 帮教工作信息管理 - 年度鉴定 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param map map
     * @return String
     */
    @Override
    public String getJdInfoList(Integer pageIndex,Integer pageSize,Map<String,Object> map) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        return PageReturnUtil.pageReturn(page.setRecords(abPrisonerMapper.getJdInfoList(page,map)));
    }
}